package com.hcinfos.gsoo.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.TimeGridViewAdapter;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.view.TagViewGroup;

public class DatePickerActivity extends Activity implements OnClickListener {

	private GridView tagView;
	private ImageView back_iv;

	private List<String> timeList;
	private TimeGridViewAdapter adapter;
	private LinearLayout time_ll;
	private int selectDay = 0;
	private List<String> currentDayList;
	private String date = null;
	private int year;
	private Button ok_bt;
	private String currentDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_date_picker);
		Date d1 = Calendar.getInstance().getTime();
		currentDate = new SimpleDateFormat("yyyy" + "-" + "MM" + "-" + "dd ")
				.format(d1);
		ok_bt = (Button) findViewById(R.id.ok_bt);
		ok_bt.setOnClickListener(this);
		currentDayList = new ArrayList<String>();
		time_ll = (LinearLayout) findViewById(R.id.time_ll);
		tagView = (GridView) findViewById(R.id.tagView);
		back_iv = (ImageView) findViewById(R.id.back_iv);
		back_iv.setOnClickListener(this);

		timeList = new ArrayList<String>();
		adapter = new TimeGridViewAdapter(this, timeList);
		tagView.setAdapter(adapter);

		testData();
		currentDateList();
		tagView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				adapter.setPosition(position);
			}
		});
		initViews();
		Calendar calen = Calendar.getInstance();
		year = calen.get(Calendar.YEAR);

		adapter.setDataChange(currentDayList);
		if (currentDayList.size() > 0) {
			adapter.setPosition(0);
			date = year + "-" + day + "," + currentDayList.get(0);
		} else {
			String min = "";
			if (calen.get(Calendar.MINUTE) < 10) {
				min = "0" + calen.get(Calendar.MINUTE);
			} else {
				min = calen.get(Calendar.MINUTE) + "";
			}
			date = year + "-" + day + "," + calen.get(Calendar.HOUR) + ":"
					+ min;
		}

		tagView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				if (selectDay == 0) {
					date = year + "-" + day + "," + currentDayList.get(arg2);
				} else if (selectDay == 1) {
					date = year + "-" + tor + "," + timeList.get(arg2);
				} else {
					date = year + "-" + torStep + "," + timeList.get(arg2);

				}
				adapter.setPosition(arg2);
			}
		});
	}

	private String day = "";
	private String tor = "";
	private String torStep = "";
	private String name;
	private String tel;

	private void initViews() {
		time_ll.removeAllViews();
		LayoutInflater lif = getLayoutInflater();
		SimpleDateFormat f = new SimpleDateFormat("MM-dd");
		// 新建一个日历对象。注意：类Calendar是抽象的要使用getInstance()实例化，或者实例化其子类
		for (int i = 0; i <= 2; i++) {
			Calendar calen = Calendar.getInstance();
			calen.add(Calendar.DAY_OF_YEAR, i);
			Date c = calen.getTime();
			View view = lif.inflate(R.layout.date_picker_item, null);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT);
			params.weight = 1;
			view.setLayoutParams(params);
			TextView day_tv = (TextView) view.findViewById(R.id.day_tv);
			TextView date_tv = (TextView) view.findViewById(R.id.date_tv);
			final ImageView line_iv = (ImageView) view
					.findViewById(R.id.line_iv);
			time_ll.addView(view);
			date_tv.setText(f.format(c));
			switch (i) {
			case 0:
				day_tv.setText("今天");
				day = f.format(c);
				line_iv.setVisibility(View.VISIBLE);
				break;
			case 1:
				day_tv.setText("明天");
				tor = f.format(c);
				line_iv.setVisibility(View.GONE);
				break;
			case 2:
				torStep = f.format(c);
				day_tv.setText("后天");
				line_iv.setVisibility(View.GONE);
				break;
			}
			view.setTag(i);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					int b = (Integer) v.getTag();
					if (b == 0) {
						selectDay = 0;
						adapter.setDataChange(currentDayList);
						date = year + "-" + day + "," + currentDayList.get(0);
					} else if (b == 1) {
						selectDay = 1;
						adapter.setDataChange(timeList);
						date = year + "-" + tor + "," + timeList.get(0);

					} else {
						selectDay = 2;
						adapter.setDataChange(timeList);
						date = year + "-" + torStep + "," + timeList.get(0);

					}

					for (int a = 0; a <= 2; a++) {
						View contentView = time_ll.getChildAt(a);
						ImageView inVisibleView = (ImageView) contentView
								.findViewById(R.id.line_iv);
						inVisibleView.setVisibility(View.GONE);

					}
					ImageView line = (ImageView) v.findViewById(R.id.line_iv);
					line.setVisibility(View.VISIBLE);
				}
			});

		}

	}

	private void currentDateList() {
		try {

			Date d1 = Calendar.getInstance().getTime();
			String s1 = new SimpleDateFormat("yyyy" + "-" + "MM" + "-"
					+ "dd HH" + ":" + "mm").format(d1);

			for (String str : timeList) {

				String time = currentDate + " " + str;
				LogUtil.d("wxf", time);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date dd = sdf.parse(time.toString());
				String ss = new SimpleDateFormat("yyyy" + "-" + "MM" + "-"
						+ "dd HH" + ":" + "mm").format(dd);
				int i = d1.compareTo(dd);
				if (i <= 0) {
					currentDayList.add(str);
				} else {
				}

			}
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
	}

	private void testData() {
		timeList.add("11:00");
		timeList.add("11:30");
		timeList.add("12:00");
		timeList.add("12:30");
		timeList.add("13:00");
		timeList.add("13:30");
		timeList.add("14:00");
		timeList.add("14:30");
		timeList.add("17:00");
		timeList.add("17:30");
		timeList.add("18:00");
		timeList.add("18:30");
		timeList.add("19:00");
		timeList.add("19:30");
		timeList.add("20:00");
		timeList.add("20:30");
		timeList.add("21:00");

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back_iv:
			finish();
			break;
		case R.id.ok_bt:
			Intent intent = new Intent();
			intent.putExtra("date", date);

			setResult(Activity.RESULT_OK, intent);
			finish();
			break;
		}

	}
}
